package li.kaeppe.travel.tracker.web.backing;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import li.kaeppe.travel.tracker.domain.Trip;
import li.kaeppe.travel.tracker.service.TripService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ListTripBacking {
	
	/* **************************************************** */
	/*                      CONSTANTS                       */
	/* **************************************************** */
	
	private static final Log log = LogFactory.getLog(ListTripBacking.class);
	
	/* **************************************************** */
	/*                     MEMBER FIELDS                    */
	/* **************************************************** */

	private TripService travelTrackerService;
	
	private Trip tripBean;
	
	/* **************************************************** */
	/*                      CONSTRUCTOR                     */
	/* **************************************************** */

	/* **************************************************** */
	/*                    PUBLIC METHODS                    */
	/* **************************************************** */
	
	public List<Trip> getAllTrips() {
		if(log.isDebugEnabled()) {
			log.debug("ListTripBacking -> getAllTrips()");
		}
		return this.getTravelTrackerService().getAllTrips();
	}
	
	public List<SelectItem> getAllTripsOptions() {
		if(log.isDebugEnabled()) {
			log.debug("ListTripBacking -> getAllTripsOptions()");
		}
		
		List<SelectItem> items = new ArrayList<SelectItem>();
		
		for(Trip trip : this.getAllTrips()) {
			items.add(new SelectItem(new Long(trip.getId()), trip.getTitle(), trip.getDescription()));
		}
		return items;
	}
	
	public String saveTrip(){
		this.getTravelTrackerService().storeTrip(this.getTripBean());
		this.setTripBean(new Trip());
		return "success";
	}
	
	/* **************************************************** */
	/*                  PROTECTED METHODS                   */
	/* **************************************************** */

	/* **************************************************** */
	/*                   PRIVATE METHODS                    */
	/* **************************************************** */

	/* **************************************************** */
	/*                  GETTERS AND SETTERS                 */
	/* **************************************************** */
	
	public TripService getTravelTrackerService() {
		return travelTrackerService;
	}

	public void setTravelTrackerService(TripService travelTrackerService) {
		this.travelTrackerService = travelTrackerService;
	}
	
	public Trip getTripBean() {
		return tripBean;
	}

	public void setTripBean(Trip tripBean) {
		this.tripBean = tripBean;
	}
	
}
